package cn.com.nxt.txb;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import cn.com.nxt.txb.httpdelegate.LinkDelegate;
import cn.com.nxt.txb.util.AppUtil;

@ContentView(R.layout.activity_about)
public class AboutActivity extends BaseActivity {
    @ViewInject(R.id.tv_version)
    private TextView mVersionTv;
    private LinkDelegate linkDelegate;
    private byte linkFlag = (byte) 0x86;
    private String download;
    private String function;
    private String usedesc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解绑定
        mVersionTv.setText(getVersion(AboutActivity.this));
        linkDelegate=new LinkDelegate(this);
        linkDelegate.linkerAddrs(linkFlag);
    }
    // 获取当前版本的版本号
    public String getVersion(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }
    @Event({R.id.ll_feedback,R.id.ll_download,R.id.ll_function_introduced,R.id.ll_directions_use})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.ll_feedback:
                skipFeedback();
                break;
            case R.id.ll_download:
                download();
                break;
            case R.id.ll_function_introduced:
                functionIntroduced();
                break;
            case R.id.ll_directions_use:
                directionsUse();
                break;
        }
    }

    private void directionsUse(){
        if(AppUtil.isBlank(usedesc))
            return;
        Intent intent = new Intent(AboutActivity.this,
                ActivityDetails.class);
        intent.putExtra("url", usedesc);
        intent.putExtra("type", "使用说明");
        startActivity(intent);
    }

    private void functionIntroduced(){
        if(AppUtil.isBlank(function))
            return;
        Intent intent = new Intent(AboutActivity.this,
                ActivityDetails.class);
        intent.putExtra("url", function);
        intent.putExtra("type", "功能介绍");
        startActivity(intent);
    }

    private void  skipFeedback(){
        startActivity(new Intent(AboutActivity.this, FeedbackActivity.class));
    }
    private void  download(){
        if(AppUtil.isBlank(download))
            return;
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(download);
        intent.setData(content_url);
        startActivity(intent);
//        Intent intent = new Intent(AboutActivity.this,
//                DownloadActivity.class);
//        intent.putExtra("url", download);
//        startActivity(intent);
    }

    @Override
    public void success(String result, byte flag) {
        super.success(result, flag);
        try {
            JSONObject obj=new JSONObject(result);
            download=obj.optString("download");
            function=obj.optString("function");
            usedesc=obj.optString("usedesc");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
